package com.blink.optimize.apis.collect;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * <Description> </Description>
 * <ClassName> TestCompareVectorAndHashTable</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月08日 11:09
 */
public class TestCompareVectorAndHashTable {
    @Test
    public void testVector() {
        Vector vector1 = new Vector();//默认initialCapacity为10，capacityIncrement为0，超过10后开始成倍扩容,可能会浪费空间
        Vector vector2 = new Vector(16);//指定向量大小16，默认capacityIncrement为0，，超过16后开始成倍扩容,可能会浪费空间
        Vector vector3 = new Vector(16, 10);////指定向量大小16，capacityIncrement为10，，超过16后开始扩容，每次扩10个
        vector1.add(new Object());
        vector2.add(new Object());
        vector3.add(new Object());
        //尽量减少使用Vector,它是线程安全的
        ArrayList arrayList = new ArrayList();
        List list = Collections.synchronizedList(arrayList);
        list.add(new Object());
        //class Stack<E> extends Vector<E>,提倡通过LinkedList自己实现栈
        Stack stack = new Stack();
        stack.add(new Object());
    }

    @Test
    public void testHashTable() {
        //线程安全的,value不能为null,不推荐使用
        Hashtable hashtable = new Hashtable();
        hashtable.put("A", "A");
//        hashtable.put(null, "A");//java.lang.NullPointerException
//        hashtable.put("A", null);//java.lang.NullPointerException
        HashMap hashMap = new HashMap();
        hashMap.put("A", "A");
        hashMap.put(null, "A");//java.lang.NullPointerException
        hashMap.put("A", null);//java.lang.NullPointerException

    }
}
